package com.tianpengtech.admin.fairsalary;
import org.eclipse.jetty.security.UserAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairsalary.FairSalary;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.XxxUtil;

/**
* 路由配置地址：add("/admin/fairsalary", FairSalaryAdminController.class, "/admin/fairsalary");
* 数据库映射地址：arp.addMapping(FairSalary.dao.tableName(),FairSalary.class);
*/
@Before(InitInterceptor.class)
public class FairSalaryAdminController extends AbstractAdminController{

	@Override
	protected void config() {
		this.clazz=FairSalary.class;
		this.service=new FairSalaryAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	@Override
	public void parsePageRecord(Object results) {
		FairSalary salary = (FairSalary)results;
		salary.set(FairSalary.SALARY, salary.fee());
	}
	
	
	public void charge(){
		int id = getParaToInt(FairSalary.ID,0);
		String sql="select t1.*,t2.company_name from fair_user_salary as t1,fair_user_authenticate as t2 where t1.user_id = t2.user_id and t1.id = ?";
		setAttr("fairSalary",FairSalary.dao.findFirst(sql,id));
		renderTemplate("charge.html");
	}
	
	
	public void doCharge(){
		int id = getParaToInt(FairSalary.ID,0);
		int fee = getParaToInt("fee",0);
		String company_name = getPara("company_name","");
		String reason = getPara("reason","");
		fee = fee*100;//单位转换成分
		FairSalary salary= FairSalary.dao.findById(id);
		int userFee = salary.getInt(FairSalary.SALARY);
		int leftfee = userFee-fee;
		if(leftfee<0){
			renderJson(JsonUtil.printError("余额不足，不能进行当前操作"));
			return;
		}else{
			salary.set(FairSalary.SALARY, leftfee).update();
			Account account = XxxUtil.readSessionInfo();
			long userId = account.getLong(Account.ID);
			String result="管理员扣除广告商:"+company_name+"的消费费用,金额为:￥"+(fee/100);
			OperationDao.ME.recordLog(id, Opertion.OPT_FAIR_USER_SALARY_CHARGE,userId,result);
			renderJson(JsonUtil.printError("扣款成功"));
			return;
		}
	}
}

	